package com.ibm.risk.irmp.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String today() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date today = new Date();
        String todayString = sdf.format(today);
        return todayString;
    }

    public static String getDayStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年 MM月 dd日");
        Date today = new Date();
        String todayString = sdf.format(today);
        return todayString;
    }

    public static Integer getYearStr(String dataDate) {
        return Integer.valueOf(dataDate.substring(0, 4));
//        return todayString;
    }

    public static String getDayStr(String dataDate) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年 MM月 dd日");
        String todayString = null;
        try {
            todayString = sdf2.format(sdf1.parse(dataDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return todayString;
    }

    public static String getMonthStr(String dataDate) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年 MM月");
        String todayString = null;
        try {
            todayString = sdf2.format(sdf1.parse(dataDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return todayString;
    }

    public static BigDecimal diffYears(LocalDate startDate, LocalDate endDate) {
        if (startDate == null || endDate == null) {
            throw new RuntimeException("开始日期或者结束日期不能为空");
        }
        Period period = Period.between(startDate, endDate);
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();
        return (new BigDecimal(years + months / 12f + days / 365f)).setScale(4, RoundingMode.HALF_UP);
    }


    /**
     * 获取期次
     */
    public static String getQuarter(Integer offsetMonth) {
        String quarter = "";

        final LocalDate date = LocalDate.now().plusMonths(offsetMonth);

        // 年和月份
        int year = date.getYear();
        int month = date.getMonth().getValue();
        if (month >= 1 && month <= 3) {
            quarter = "Q1";
        } else if (month >= 4 && month <= 6) {
            quarter = "Q2";
        } else if (month >= 7 && month <= 9) {
            quarter = "Q3";
        } else if (month >= 10 && month <= 12) {
            quarter = "Q4";
        }
        return year + quarter;
    }

    public static String getQuarterDesc(String quarter) {
        String quarterDesc = "";
        if (StringUtils.endsWith(quarter, "Q1")) {
            quarterDesc = StringUtils.replace(quarter, "Q1", "年一季度");
        } else if (StringUtils.endsWith(quarter, "Q2")) {
            quarterDesc = StringUtils.replace(quarter, "Q2", "年二季度");
        } else if (StringUtils.endsWith(quarter, "Q3")) {
            quarterDesc = StringUtils.replace(quarter, "Q3", "年三季度");
        } else if (StringUtils.endsWith(quarter, "Q4")) {
            quarterDesc = StringUtils.replace(quarter, "Q4", "年四季度");
        }
        return quarterDesc;
    }

}
